package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.GroupApplyBean;
import com.banzhi.shop.data.GroupTypeBean;
import com.banzhi.shop.data.GroupingDetailsBean;
import com.banzhi.shop.model.CallBack.ConfirmOrderTypeCallBack;
import com.banzhi.shop.model.GroupApplyCallBack;
import com.banzhi.shop.model.Impl.ConfirmOrderTypeImpl;
import com.banzhi.shop.model.Impl.GroupApplyImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.moor.imkf.ormlite.stmt.query.In;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.security.acl.Group;

import static com.banzhi.shop.MyApp.context;

//确认订单(拿利润)
public class ConfirmOrderProfitActivity extends BaseActivity implements View.OnClickListener, ConfirmOrderTypeCallBack, GroupApplyCallBack {

    private ImageView mIvBack;
    private ImageView mIvGoods;
    private TextView mTvOpenPrice;
    private TextView mTvGroupProfit;
    private TextView mTvGroupNum;
    private TextView mTvGroupPrice;
    private TextView mTvTitle;
    private TextView mTvPay;
    private ConfirmOrderTypeImpl mModel;
    private String mGoodId;
    private GroupTypeBean mGroupTypeBean;
    private GroupApplyImpl mGroupApplyModel;
    private GroupApplyBean mGroupApplyBean;
    private String mType;
    private TextView mTvGuigeTitle;
    private String mGuigetitle;
    private String mOptionid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order_profit);
        Translucent.setTranslucent(this);
        initView();
        setListener();

        Intent intent = getIntent();
        mGoodId = intent.getStringExtra("mGoodId");
        mType = intent.getStringExtra("type");
        mGuigetitle = intent.getStringExtra("guigetitle");
        mOptionid = intent.getStringExtra("optionid");

        mModel = new ConfirmOrderTypeImpl();
        mModel.getConfirmOrderTypeData(mGoodId,mType,this);

        mGroupApplyModel = new GroupApplyImpl();


    }

    private void initView() {
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvGoods = (ImageView) findViewById(R.id.iv_goods);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvGroupPrice = (TextView) findViewById(R.id.tv_group_price);
        mTvGroupNum = (TextView) findViewById(R.id.tv_group_num);
        mTvGroupProfit = (TextView) findViewById(R.id.tv_group_profit);
        mTvOpenPrice = (TextView) findViewById(R.id.tv_open_price);
        mTvPay = (TextView) findViewById(R.id.tv_pay);
        mTvGuigeTitle = (TextView) findViewById(R.id.tv_guigetitle);

    }

    private void setListener() {
        mIvBack.setOnClickListener(this);
        mTvPay.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
           case R.id.tv_pay:
               mGroupApplyModel.getGroupApplyProfitData(mOptionid,mGoodId,mType,this);
               //跳转支付页面
                break;
        }

    }

    @Override
    public void onGetConfirmOrderTypeDataSuccess(Object object) {
        mGroupTypeBean = (GroupTypeBean) object;
        showView();
    }

    private void showView() {
        mTvGuigeTitle.setText(mGuigetitle);

        if (ASUtils.isCanUse(mGroupTypeBean.getTitle())) {
            mTvTitle.setText(mGroupTypeBean.getTitle());
        }
        if (ASUtils.isCanUse(mGroupTypeBean.getGroupsprice())) {
            mTvGroupPrice.setText(mGroupTypeBean.getGroupsprice());
        }
        if (ASUtils.isCanUse(mGroupTypeBean.getGroupnum())) {
            mTvGroupNum.setText(mGroupTypeBean.getGroupnum());
        }
        if (ASUtils.isCanUse(mGroupTypeBean.getGroupsprice())) {
            mTvGroupProfit.setText(mGroupTypeBean.getGroupsprice());
        }
        if (ASUtils.isCanUse(mGroupTypeBean.getOpenprice())) {
            mTvOpenPrice.setText(mGroupTypeBean.getOpenprice());
        }

        if (ASUtils.isCanUse(mGroupTypeBean.getThumb_url().get(0))) {
            Picasso.with(context).load(mGroupTypeBean.getThumb_url().get(0)).into(mIvGoods);
        }
    }

    @Override
    public void onGetConfirmOrderTypeDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    @Override
    public void onGetGroupApplyDataSuccess(Object object) {
        mGroupApplyBean = (GroupApplyBean) object;
        Intent intent = new Intent(this,CheckstandGroupActivity.class);
        intent.putExtra("orderno",mGroupApplyBean.getOrderno());
        intent.putExtra("balance",mGroupApplyBean.getBanlance());
        intent.putExtra("price",mGroupApplyBean.getPrice());
        startActivity(intent);

    }

    @Override
    public void onGetGroupApplyDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }
}
